<?php
require "common.php";

//$res =all("select * from `article`");
//foreach ($res as $k=>$v){
//    $name = one("select * from `category` where id ='$v[category_id]'");
//    $res[$k]['name'] = $name['name'];
//}
//$smarty->assign('articles',$res);
//$smarty->display('index.html');

class IndexController extends CommonController{
    function index(){
        $article = $this->all("select * from article");
        foreach ($article as $k=>$v){
            $name = $this->one("select * from category where id ='$v[category_id]'");
            $article[$k]['name']=$name['name'];
        }
        $categories =$this->all("select * from category");
        foreach ($categories as $k=>$v){
            $num = $this->getNum("select * from article where category_id='$v[id]'");
            $categories[$k]['num']=$num;
        }
        $last_articles=$this->all("select * from article order by time desc limit 0,3");
        foreach ($last_articles as $k=>$v){
            $commentNum = $this->getNum("select * from comment where pid='$v[id]'");
            $last_articles[$k]['num']=$commentNum;
        }
        $comments = $this->all("select * from comment order by time desc limit 0,3");
//        $this->dump($categories);
//        exit;
        $this->assign('total',compact('categories','last_articles','comments'));
        $this->assign('articles',$article);
        $this->display('index.html');
    }
}
$index = new IndexController();
$index->index();